// Constructs an income panel for couples with DV report 
global data_folder "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results_revision"


use "W:\intimate\data\allwomen_data_clean_withDV", clear

// cleaning 
g insample = 0
replace insample=1 if dv_couple==1
replace insample=1 if dv_couple==0 & victim_events==0 & suspect_events==0
keep if insample==1
drop insample

replace nchildB1=5 if nchildB1>=5

gen language=(kieli=="fi")
gen spouse_language=(spouse_kieli=="fi")

// merge crime
g year_event = year_start_cohab-1
merge 1:1 shnro year_event using "W:\intimate\data\cumul_crime_violent"
drop if _merge==2
replace cum_crimes = 0 if _merge==1
drop _merge sphnro
ren cum_crimes pre_crimes

g sphnro = sphnro0
merge 1:1 sphnro year_event using "W:\intimate\data\cumul_crime_violent", keepusing(cum_crimes)
drop if _merge==2
replace cum_crimes = 0 if _merge==1
drop _merge sphnro
ren cum_crimes spouse_pre_crimes

replace pre_crimes = 5 if pre_crimes>5
replace spouse_pre_crimes = 5 if spouse_pre_crimes>5


gen spouse_college=spouse_pre_educ>=6
gen spouse_high_school=spouse_pre_educ>=3 & spouse_pre_educ<=5
gen spouse_ho=spouse_pre_educ<3

gen college=educ>=6
gen high_school=educ>=3 & educ<=5
gen ho=educ<3

forvalues y=2005/2015{
    preserve 
keep if year_event==`y'
// merge age of youngest child
merge 1:1 shnro year_event using "$data_folder\fertility_`y'.dta", keepusing(age_yst)
drop if _merge==2
replace age_yst = -1 if _merge==1
replace age_yst = -1 if age_yst==.
drop _merge 


// merge relationship history 
merge 1:1 shnro year_event using "$data_folder\relationship_hist_`y'.dta", keepusing(relation_duration ndistinct avg_relation_year)
drop if _merge==2
replace ndistinct = 0 if _merge==1
replace relation_duration = 0 if _merge==1
replace avg_relation_year = 0 if _merge==1
drop _merge 



// merge year since graduation
merge 1:1 shnro year_event using "$data_folder\year_since_graduation_`y'.dta", keepusing(yr_since_graduation)
drop if _merge==2
replace yr_since_graduation = 0 if _merge==1
drop _merge 

// merge mental health prescriptions 
merge 1:1 shnro year_event using "W:\intimate\data\prescriptions_depression_indlevel.dta", keepusing(depression)
drop if _merge==2
replace depression=0 if _merge==1 
drop _merge 

tempfile t`y'
save `t`y'', replace 
restore 
}

clear 
forvalues y=2005/2015{
    append using `t`y''
}

// split into groups
g data_group = .
replace data_group =1 if ptoim1B1 ==0 & nchildB1==0 & spouse_pre_ptoim1==0 & college==0
replace data_group =2 if ptoim1B1 ==0 & nchildB1==0 & spouse_pre_ptoim1==0 & college==1
replace data_group =3 if ptoim1B1 ==0 & nchildB1==0 & spouse_pre_ptoim1==1 & college==0
replace data_group =4 if ptoim1B1 ==0 & nchildB1==0 & spouse_pre_ptoim1==1 & college==1
replace data_group =5 if ptoim1B1 ==0 & nchildB1>0 & spouse_pre_ptoim1==0 & college==0
replace data_group =5 if ptoim1B1 ==0 & nchildB1>0 & spouse_pre_ptoim1==0 & college==1
replace data_group =6 if ptoim1B1 ==0 & nchildB1>0 & spouse_pre_ptoim1==1 & college==0
replace data_group =7 if ptoim1B1 ==0 & nchildB1>0 & spouse_pre_ptoim1==1 & college==1
replace data_group =8 if ptoim1B1 ==1 & tyotuB1 < 20000 & nchildB1==0 & spouse_pre_ptoim1==0 & college==0
replace data_group =9 if ptoim1B1 ==1 & tyotuB1 < 20000 & nchildB1==0 & spouse_pre_ptoim1==0 & college==1
replace data_group =10 if ptoim1B1 ==1 & tyotuB1 < 20000 & nchildB1==0 & spouse_pre_ptoim1==1 & college==0
replace data_group =11 if ptoim1B1 ==1 & tyotuB1 < 20000 & nchildB1==0 & spouse_pre_ptoim1==1 & college==1
replace data_group =12 if ptoim1B1 ==1 & tyotuB1 < 20000 & nchildB1>0 & spouse_pre_ptoim1==0 & college==0
replace data_group =12 if ptoim1B1 ==1 & tyotuB1 < 20000 & nchildB1>0 & spouse_pre_ptoim1==0 & college==1
replace data_group =13 if ptoim1B1 ==1 & tyotuB1 < 20000 & nchildB1>0 & spouse_pre_ptoim1==1 & college==0
replace data_group =14 if ptoim1B1 ==1 & tyotuB1 < 20000 & nchildB1>0 & spouse_pre_ptoim1==1 & college==1
replace data_group =14 if ptoim1B1 ==1 & tyotuB1 >= 20000 & nchildB1==0 & spouse_pre_ptoim1==0 & college==0
replace data_group =15 if ptoim1B1 ==1 & tyotuB1 >= 20000 & nchildB1==0 & spouse_pre_ptoim1==0 & college==1
replace data_group =16 if ptoim1B1 ==1 & tyotuB1 >= 20000 & nchildB1==0 & spouse_pre_ptoim1==1 & college==0
replace data_group =17 if ptoim1B1 ==1 & tyotuB1 >= 20000 & nchildB1==0 & spouse_pre_ptoim1==1 & college==1
replace data_group =18 if ptoim1B1 ==1 & tyotuB1 >= 20000 & nchildB1>0 & spouse_pre_ptoim1==0 & college==0
replace data_group =19 if ptoim1B1 ==1 & tyotuB1 >= 20000 & nchildB1>0 & spouse_pre_ptoim1==0 & college==1
replace data_group =20 if ptoim1B1 ==1 & tyotuB1 >= 20000 & nchildB1>0 & spouse_pre_ptoim1==1 & college==0
replace data_group =21 if ptoim1B1 ==1 & tyotuB1 >= 20000 & nchildB1>0 & spouse_pre_ptoim1==1 & college==1


sort dv_couple data_group shnro year_start_cohab

// order for graph 
g graph_data_group = .
replace graph_data_group = 1 if data_group == 1
replace graph_data_group = 2 if data_group == 5
replace graph_data_group = 3 if data_group ==20
replace graph_data_group =4 if data_group ==3
replace graph_data_group =5 if data_group ==6
replace graph_data_group =6 if data_group ==10
replace graph_data_group =7 if data_group ==13
replace graph_data_group =8 if data_group ==16
replace graph_data_group =9 if data_group ==8
replace graph_data_group =10 if data_group ==12
replace graph_data_group =11 if data_group ==18
replace graph_data_group =12 if data_group ==14
replace graph_data_group =13 if data_group ==17
replace graph_data_group =14 if data_group ==21
replace graph_data_group =15 if data_group ==11
replace graph_data_group =16 if data_group ==19
replace graph_data_group =17 if data_group ==15
replace graph_data_group =18 if data_group ==4
replace graph_data_group =19 if data_group ==7
replace graph_data_group =20 if data_group ==2
replace graph_data_group =21 if data_group ==9

twoway (hist graph_data_group if dv_couple==1, fcolor(red%50) lcolor(red) width(1) start(1)) ///
		(hist graph_data_group if dv_couple == 0, fcolor(blue%50) lcolor(blue) width(1) start(1)) , ///
		 graphregion(color(white)) ytitle("Density") xtitle("Exact Match Group")  legend(order(1 "Non DV Couple" 2 "DV Couple")) ///
		 xlabel(1(1)21)
graph export "$result_folder\hist_exact_match_group.pdf", replace	

// create prop scores 

forvalues i = 1/21 {
    preserve
	keep if data_group==`i'
	save "$data_folder\match_data_group`i'", replace
	restore
}

forvalues i = 1/21 {

    
	use "$data_folder\match_data_group`i'", clear
	
	set seed 12345
	sort dv_couple data_group shnro year_start_cohab
	
	forvalues s = 1/4 {
		xtile inc_group`s' = tyotuB`s', nq(5)
		g cohabit`s' = 1 - missing(sphnroB`s')
	}
	g age_group = floor(age/5)
	replace age_yst=100 if age_yst==-1
	g age_yst_group=floor(age_yst/5)
	replace ndistinct=3 if ndistinct>3
	g avg_relation_group=floor(avg_relation_year/3)

	g spouse_missing = 0
	replace spouse_missing = 1 if missing(spouse_pre_age)
	xtile spouse_group1 = spouse_pre_tyotu, nq(5)
	replace spouse_group1 = 0 if spouse_missing==1
	g spouse_age = floor(spouse_pre_age/5)
	replace spouse_age = 0 if spouse_missing==1
	replace spouse_pre_ptoim1 = 0 if spouse_missing==1
	
	g yr_since_grad_group=floor(yr_since_graduation/5)

	logit dv_couple i.age_group ho ///
				i.inc_group1 i.ptoim1B1 ///
				i.inc_group2 i.ptoim1B2 ///
				i.inc_group3 i.ptoim1B3 ///
				i.spouse_age spouse_college spouse_ho i.spouse_group1 i.nchildB1 ///
				i.spouse_missing i.year_start_cohab ///
				cohabit* ///
				i.inc_group4 i.ptoim1B4 ///
				i.age_yst_group i.ndistinct i.avg_relation_group i.yr_since_grad_group ///
				i.depression
			
	predict dv_prob, pr
	g missing_prob = missing(dv_prob)
	table missing_prob dv_couple
	drop if missing_prob==1
	
	keep dv_couple dv_prob data_group shnro year_start_cohab 
	save "$data_folder\match_pscore_`i'", replace
	
	
}

use "$data_folder\match_pscore_1", clear
forvalues i = 2 /21 {
    append using "$data_folder\match_pscore_`i'"
}
save "$data_folder\match_pscore", replace

use "W:\intimate\data\match_victim_data_clean_new", clear
merge m:1 shnro year_start_cohab using "$data_folder\match_pscore"


set scheme s2mono

twoway (kdensity dv_prob if dv_couple==1) (kdensity dv_prob if dv_couple==0), ///
	graphregion(color(white)) ytitle("Density") xtitle("Propensity Score") ///
	legend(order(1 "DV Couple" 2 "Non DV Couple"))
graph export "$result_folder\prob_score_dist_full_sample", replace


twoway (kdensity dv_prob if dv_couple==1 & _merge==3) ///
		(kdensity dv_prob if dv_couple==0) ///
		(kdensity dv_prob if dv_couple==0 & _merge ==3, lcolor(red)), ///
	graphregion(color(white)) ytitle("Density") xtitle("Propensity Score") ///
	legend(order(1 "DV Couple" 2 "All Non-DV" 3 "Matched Non-DV"))
graph export "$result_folder\prob_score_dist_matched_sample.pdf", replace






